<template>
  <div class="container">
    <el-dialog
      :title="form.id ? '修改目录' : '新增目录'"
      :visible.sync="showDirectorys"
      width="25%"
      :before-close="btnCancel"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="所属学科" v-if="!subjectIds">
          <el-select v-model="form.subjectID">
            <el-option
              v-for="item in subjectList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="目录名称">
          <el-input
            v-model="form.directoryName"
            placeholder="请输入目录名称"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="btnCancel">取 消</el-button>
        <el-button type="primary" @click="btnOk">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { simple } from '@/api/hmmm/subjects'
import { add, update } from '@/api/hmmm/directorys'
export default {
  props: {
    showDirectorys: {
      type: Boolean,
      required: true
    },
    subjectIds: null
  },
  data () {
    return {
      subjectList: [],
      form: {
        subjectID: '',
        directoryName: '',
        id: null
      },
      rules: {
        subjectID: [
          { required: true, message: '请选择活所属学科', trigger: 'change' }
        ],
        directoryName: [
          { required: true, message: '请输入目录名称', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    btnCancel () {
      this.form.subjectID = ''
      this.form.directoryName = ''
      this.$emit('closeDirectorys', false)
    },
    async btnOk () {
      if (this.form.id) {
        await update(this.form)
        this.$emit('closeDirectorys', false)
        this.$parent.getDirectoryList()
      } else {
        await add(this.form)
        this.$emit('closeDirectorys', false)
        this.$parent.getDirectoryList()
      }
    },
    // 获取学科列表
    async getSubjectList () {
      const { data } = await simple()
      this.subjectList = data
    },
    chuancan (row) {
      this.form.id = row.row.id
      this.form.subjectID = row.row.subjectID
      this.form.directoryName = row.row.directoryName
    }
  },
  created () {
    this.getSubjectList()
  }
}
</script>

<style scoped lang='scss'>
</style>
